package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.AuditRecordDao;
import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.AuditRecord;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuditRecordServiceImpl implements AuditRecordService {

    @Autowired
    private AuditRecordDao auditRecordDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Result getAuditRecord(Integer relatedType, Integer relatedId, Integer page, Integer pageSize) {
        try {
            if (pageSize == null || !CommonUtils.isNumeric(pageSize.toString())) {
                return ResultUtil.error(500, "错误参数pageSize", "Error Param pageSize", null);
            }
            if (page == null || !CommonUtils.isNumeric(page.toString())) {
                return ResultUtil.error(500, "错误参数page", "Error Param page", null);
            }
            int begin = (page - 1) * pageSize;

            List<AuditRecord> list1 = auditRecordDao.getAuditRecord(relatedType, relatedId, begin, pageSize);
            for (AuditRecord auditRecord : list1) {
                auditRecord.setCreateUser(userDao.getUserInfoById(auditRecord.getCreateUserId()));
            }

            List<AuditRecord> list2 = auditRecordDao.getAuditRecord(relatedType, relatedId, null, null);

            return ResultUtil.tableSuccess(page, pageSize, list2.size(), list1);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public void addAuditRecord(AuditRecord auditRecord) {
        try {
            auditRecordDao.addAuditRecord(auditRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
